package com.example.hyvelucenedemo.controller;

import com.example.hyvelucenedemo.pojo.IndexItem;
import com.example.hyvelucenedemo.pojo.SearchPageVO;
import com.example.hyvelucenedemo.utils.GouPiArticleUtil;
import com.example.hyvelucenedemo.utils.LuceneSearchUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 测试
 * @Duthor jie.zhao
 * @Date 2021/5/9 10:55 上午
 */
@Slf4j
@Controller
@RequestMapping("/test")
public class TestController {

    private static int num = 1;

    @Autowired
    private LuceneSearchUtil luceneSearchUtil;

    @RequestMapping("/buildIndex")
    @ResponseBody
    public String buildIndex() {
        long now = System.currentTimeMillis();
        List<String> articleNameList = new ArrayList<>();
        articleNameList.add("三体");
        articleNameList.add("这是计划的一部分");
        articleNameList.add("什么是四块五的妞");
        articleNameList.add("关于你的风景");
        articleNameList.add("最后一只猫");
        articleNameList.add("虎牙烤羊腿");
        articleNameList.add("吃鸡不求人");
        articleNameList.add("白胡子老头");
        articleNameList.add("临沂喷香哥");
        articleNameList.add("小甜甜");

        for (int i = 0; i < articleNameList.size(); i++) {
            String title = articleNameList.get(i);
            IndexItem indexItem = new IndexItem();
            indexItem.setOnlyId(System.currentTimeMillis() + "");
            indexItem.setId(i + "");
            indexItem.setType("1");
            indexItem.setLabels("测试" + i);
            indexItem.setTitle(title);
            indexItem.setContent("我是内容" + title);
            indexItem.setContent(GouPiArticleUtil.generator(title, 2000));
            luceneSearchUtil.buildIndex(indexItem, true);
        }

        String result = "第" + num + "次构建索引成功 耗时" + (System.currentTimeMillis() - now) + "ms-----" + (System.currentTimeMillis() - now) / 1000 + "s";
        num++;
        log.info(result);
        return result;
    }


    @RequestMapping("/batchCreate")
    @ResponseBody
    public String batchCreate() {
        long now = System.currentTimeMillis();
        List<String> articleNameList = new ArrayList<>();
        articleNameList.add("三体");
        articleNameList.add("这是计划的一部分");
        articleNameList.add("什么是四块五的妞");
        articleNameList.add("关于你的风景");
        articleNameList.add("最后一只猫");
        articleNameList.add("虎牙烤羊腿");
        articleNameList.add("吃鸡不求人");
        articleNameList.add("白胡子老头");
        articleNameList.add("临沂喷香哥");
        articleNameList.add("小甜甜");

        //10w测试数据
        List<IndexItem> indexItemList = new ArrayList<>();
        for (int j = 0; j < 10000; j++) {
            for (int i = 0; i < articleNameList.size(); i++) {
                String title = articleNameList.get(i);
                IndexItem indexItem = new IndexItem();
                indexItem.setOnlyId(System.currentTimeMillis() + "");
                indexItem.setId(i + "");
                indexItem.setType("1");
                indexItem.setLabels("测试" + i);
                indexItem.setTitle(title);
                indexItem.setContent("我是内容" + title);

                indexItemList.add(indexItem);
            }
        }

        luceneSearchUtil.batchCreateIndex(indexItemList);

        String result = "第" + num + "次构建索引成功 耗时" + (System.currentTimeMillis() - now) + "ms-----" + (System.currentTimeMillis() - now) / 1000 + "s";
        num++;
        log.info(result);
        return result;
    }

    @RequestMapping("/search")
    public String search(String keyword, @RequestParam(defaultValue = "1", required = false) Integer pageNumber,
                         @RequestParam(defaultValue = "10", required = false) Integer pageSize, Model model) {
        long now = System.currentTimeMillis();
        SearchPageVO searchPageVO = luceneSearchUtil.searchPageVO(keyword, pageNumber, pageSize);
        model.addAttribute("number", searchPageVO.getNumber());
        model.addAttribute("size", searchPageVO.getSize());
        model.addAttribute("totalPages", searchPageVO.getTotalPages());
        model.addAttribute("totalElements", searchPageVO.getTotalElements());
        model.addAttribute("content", searchPageVO.getContent());
        String result = "搜索 耗时" + (System.currentTimeMillis() - now) + "ms-----" + (System.currentTimeMillis() - now) / 1000 + "s";
        log.info(result);
        return "searchList";
    }

    @RequestMapping("/updateIndex")
    @ResponseBody
    public String updateIndex(String onlyId) {
        IndexItem indexItem = new IndexItem();
        indexItem.setOnlyId(onlyId + "");
        indexItem.setId(onlyId + "");
        indexItem.setType("1");
        indexItem.setLabels("更新");
        indexItem.setTitle("权利的游戏");
        indexItem.setContent(GouPiArticleUtil.generator("权利的游戏", 2000));
        luceneSearchUtil.buildIndex(indexItem, true);
        return "更新索引";
    }

    @RequestMapping("/deleteIndex")
    @ResponseBody
    public String deleteIndex(String onlyId) {
        luceneSearchUtil.deleteIndex(onlyId);
        return "删除索引";
    }

}
